package com.sl.au.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.sl.au.entity.*;
import com.sl.au.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/*
 *  主动售后
 */

@RestController
@RequestMapping(value = "/ui/aftersaleactive")
public class AfterSaleActiveController extends BaseController {

	@Autowired
	CustomerService customerService;

	@Autowired
	NoticeRecordService noticeRecordService;

	@Autowired
	SaleAreaService saleAreaService;

	@Autowired
	OptionService optionService;

	@Autowired
	ContractService contractService;

	@Autowired
	AfterSaleActiveService afterSaleActiveService;

	@Autowired
	AfterSaleMaintainService afterSaleMaintainService;

	@Autowired
	AttachmentService attachmentService;

	@Autowired
	OperateLogService operateLogService;


	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("aftersaleactive/activeindex");
		List<Object> supplierCodes = new ArrayList<Object>();
		List<Object> codes = afterSaleActiveService.getRepository().getContractSupplierCodeList1();
		Account account=this.getMyAccount();
		String operator=account.getLoginInfo().getUserName();
		supplierCodes.add("");
		for (Object code : codes) {
			supplierCodes.add(code);
		}
		modelAndView.addObject("supplierCodes", supplierCodes);
		modelAndView.addObject("operator", operator);
		return modelAndView;
	}

	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getServicePage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		if (key == null) {
			key = "";
		}
		List<ContractVo> contracts = afterSaleActiveService.getPage(start, length, key, null, null);
		long count = afterSaleActiveService.getPageCount(key);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}



	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		List<NoticeRecord> NoticeRecords = null;
		if (org.springframework.util.StringUtils.hasText(key)) {
			NoticeRecords = noticeRecordService.getNoticeRecords(start, length, key, null, null);
		} else {
			NoticeRecords = noticeRecordService.getPage(start, length, null, null);
		}
		Long count = noticeRecordService.getRepository().count();
		map.put("data", NoticeRecords);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	@RequestMapping(value = "/save/{contractID}", method = RequestMethod.POST)
	public Map<String, Object> saveSinglePump(HttpServletRequest request,
											  @PathVariable String contractID,
											  @RequestBody AfterSaleActive afterSaleActive) {
		Map<String, Object> map = new HashMap<String, Object>();
		Contract contract = new Contract();
		contract = contractService.getRepository().findOne(contractID);
		contract.setIsAfterSaleActive(true);
		afterSaleActiveService.getRepository().save(afterSaleActive);
		contractService.getRepository().save(contract);
		map.put("code", 0);
		return map;
	}

	@RequestMapping(value = "/saveContact", method = RequestMethod.POST)
	public Map<String, Object> saveContact(HttpServletRequest request, @RequestBody NoticeRecord noticeRecord) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		//String userId=a.getUser().getId();
		User user = a.getUser();
//		direction.setUser(user);
		NoticeRecord old=noticeRecordService.getRepository().findOne(noticeRecord.getId());
		if(old!=null){
			String content  = "编辑通知记录【" + noticeRecord.getContactContent() + "】";
			operateLogService.log(user, "编辑通知记录", content);
		}else{
			String content  = "新增通知记录【" + noticeRecord.getContactContent() + "】";
			operateLogService.log(user, "新增通知记录", content);
		}
		noticeRecordService.getRepository().save(noticeRecord);
		String noticeRecordId = noticeRecord.getId();
		Date createTime = noticeRecord.getCreateTime();
		noticeRecordService.getRepository().save(noticeRecord);
		map.put("code", 0);
		map.put("noticeRecordId", noticeRecordId);
		map.put("createTime", createTime);
		return map;
	}

	/**
	 * 删除通知记录
	 *
	 * @return
	 */
	@RequestMapping(value = "/delContact", method = RequestMethod.GET)
	public Map<String, Object> delContact(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		List<NoticeRecord> noticeRecords = new ArrayList<NoticeRecord>();
		for (int i = 0; i < id.length; i++) {
			String noticeRecordId = id[i];
			NoticeRecord noticeRecord = new NoticeRecord();
			noticeRecord.setId(noticeRecordId);
			noticeRecords.add(noticeRecord);
			List<Attachment> attachments = new ArrayList<Attachment>();
			attachments = attachmentService.getRepository().findByParentId(noticeRecordId);
			attachmentService.getRepository().deleteInBatch(attachments);
		}
		/////插入删除日志
		Account a = this.getMyAccount();
		User user = a.getUser();
		for(NoticeRecord n:noticeRecords){
			NoticeRecord old=noticeRecordService.getRepository().findOne(n.getId());
			String content  = "删除通知记录【" + old.getContactContent() + "】";
			operateLogService.log(user, "删除通知记录", content);
		}

		noticeRecordService.getRepository().deleteInBatch(noticeRecords);

		map.put("code", 0);
		return map;
	}




	/**
	 * 通过当前合同下的所有ID获得所有维护计划
	 *
	 * @return
	 */
	@RequestMapping(value = "/getContractPlan", method = RequestMethod.GET)
	public Map<String, Object> getItem(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String contractID = request.getParameter("contractID");
		//String key=request.getParameter("key");
		if (contractID == null) {
			contractID = "";
		}
		List<MaintainItemDeviceVo> maintainItemDeviceVos = afterSaleActiveService.getPlanPage(start, length, contractID, null, null);
		long count = afterSaleActiveService.getPlanPageCount(contractID);
		map.put("data", maintainItemDeviceVos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	/**
	 * 处理相关的维护计划
	 *
	 * @return
	 */
	@RequestMapping(value = "/dealPlan/{ids}",method = RequestMethod.GET)
	public Map<String, Object> dealPlan(@PathVariable String ids, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Date nowTime = new Date();
		if (StringUtils.hasText(ids)) {
				String[] id = ids.split(",");
				List<MaintainItemDevice> maintainItemDevices = new ArrayList<MaintainItemDevice>();
				for (String planId : id) {
					MaintainItemDevice maintainItemDevice = afterSaleMaintainService.getRepository2().findOne(planId);
					maintainItemDevice.setStartDate(nowTime);
					maintainItemDevices.add(maintainItemDevice);
				}
				afterSaleMaintainService.getRepository2().save(maintainItemDevices);
		}
		map.put("code", 0);
		return map;
	}
}
